package services.impl;

import java.util.Date;

import model.Currency;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import services.CurrencyService;
import utils.DateUtil;

public class CurrencyServiceImpl extends HibernateGenericService<Currency, Long> implements CurrencyService {

	@Override
	public Currency findByDate(Date date) {
		Criteria criteria = this.createCriteria();
		criteria.add(Restrictions.eq("date", date));
		return (Currency) criteria.uniqueResult();
	}

	@Override
	public Currency findCurrent() {
		Currency now = this.findByDate(DateUtil.newDate());
		if(now == null) {
			Criteria criteria = this.createCriteria();
			criteria.addOrder(Order.desc("date"));
			criteria.setMaxResults(1);
			return (Currency) criteria.uniqueResult();
		}
		return now;
	}
}